package ch.zbw.sysventory.agent.socketserver.handlers;

import java.util.HashMap;
import java.util.Map;

import ch.zbw.sysventory.agent.socketserver.handlers.impl.ComputerIdRequestHandler;
import ch.zbw.sysventory.agent.socketserver.handlers.impl.LatestScanResultRequestHandler;
import ch.zbw.sysventory.agent.socketserver.handlers.impl.NewComputerIdRequestHandler;
import ch.zbw.sysventory.agent.utils.AgentPropertiesUtil;

public class ClientRequestHandlerFactory {

	private static ClientRequestHandlerFactory instance;

	public static ClientRequestHandlerFactory getInstance(AgentPropertiesUtil agentPropertiesUtil) {
		if (instance == null)
			instance = new ClientRequestHandlerFactory(agentPropertiesUtil);
		return instance;
	}

	private AgentPropertiesUtil agentPropertiesUtil;
	private Map<String, ClientRequestHandler> handlerMap;

	private ClientRequestHandlerFactory(AgentPropertiesUtil agentPropertiesUtil) {
		this.agentPropertiesUtil = agentPropertiesUtil;
		this.handlerMap = new HashMap<String, ClientRequestHandler>();
		this.handlerMap.put("ComputerIdRequest", new ComputerIdRequestHandler(this.agentPropertiesUtil));
		this.handlerMap.put("SetNewComputerIdRequest", new NewComputerIdRequestHandler(this.agentPropertiesUtil));
		this.handlerMap.put("LatestScanResultRequest", new LatestScanResultRequestHandler());
	}

	public ClientRequestHandler getRequestHandler(String message) {
		if (message != null && !"".equals(message)) {
			String key = message;
			if (message.indexOf(":") > 0) {
				key = message.substring(0, message.indexOf(":"));
			}
			return this.handlerMap.get(key);
		}
		return null;
	}

}
